/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.network;

import deatax.display.gui.HostInitPanel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pson
 */
public class Server {

    private static Server server = null;
    ServerSocket s;
    int port;
    ArrayList<ClientHandler> clients;
    boolean alive;
    ArrayList<ConnectionListener> ncListeners;

    public Server(int port) {
        this.port = port;
        clients = new ArrayList<ClientHandler>();
        alive = false;
        ncListeners = new ArrayList<ConnectionListener>();
        server = this;
        try {
            s = new ServerSocket(port);
        } catch (IOException ex) {
        }
    }

    public void kill() {
        try {
            alive = false;
            s.close();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void clear() {
        for (ClientHandler ch : clients) {
            ch.close();
        }
        clients.clear();
    }

    public void run() {
        new ConnectThread().start();
    }

    public static Server get() {
        return server;
    }

    public void addConnectListener(ConnectionListener ncl) {
        ncListeners.add(ncl);
    }

    public synchronized void sendMessages(int message_type, ArrayList<String> params) {
        StringBuilder sb = new StringBuilder();
        if (params != null) {
            for (String p : params) {
                sb.append(p).append(":");
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sendMessages(message_type + "|" + sb);
    }

    public synchronized void sendMessages(int message_type, String[] params) {
        StringBuilder sb = new StringBuilder();
        if (params != null) {
            for (String p : params) {
                sb.append(p).append(":");
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sendMessages(message_type + "|" + sb);
    }

    public synchronized void sendMessages(String line) {
        for (ClientHandler ch : clients) {
            ch.sendMessage(line);
        }
    }

    private class ConnectThread extends Thread {

        @Override
        public void run() {
            alive = true;
            try {
                if (s != null) {
                    s.setSoTimeout(5000);
                    while (alive) {
                        try {
                            Socket sock = s.accept();
                            ClientHandler ch = new ClientHandler(sock);
                            clients.add(ch);
                            for (ConnectionListener cl : ncListeners) {
                                cl.onConnection(ch);
                            }
                            ch.start();

                        } catch (IOException ex) {
                        }
                    }
                }
            } catch (SocketException ex) {
            }
        }
    }
}
